﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace EuroAudit
{
    public partial class SprAdmin : System.Web.UI.Page
    {
        GlobalF GFunc = new GlobalF();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Session["UserName"].ToString()) | Session["UserName"].ToString() == "guest")
            {
                Response.Redirect("Login.aspx", true);
                //return;
            }
            if (Session["IsAdmin"].ToString() == "1")
            {
                cmdNew.Visible = true;
                //cmdNewU.Visible = true;
            }
            else
            {
                return;
                //cmdNew.Visible = false;
                //cmdNewU.Visible = false;
            }
            if (IsPostBack)
            {
                if (_RESULT.Text == "OKcmdDel")
                {
                    cmdDel_Click(sender, e);
                    _RESULT.Text = "";
                }
            }
            LoadSpr();

        }

        void LoadSpr()
        {
            DataSet dsList;
            //DataSet dsGroupList;
            TableHeaderCell thcNewCell;
            TableHeaderRow thrNewRow;
            TableCell tcNewCell;
            TableRow trNewRow;
            Button cmdEdit;
            /*string LastStruct = "";
            string CurrStruct = "";
            string LastNod = "";
            string CurrNod = "";
            */
            //Button cmdGEdit;
            //string UsersInGroup;

            tSpr.Rows.Clear();
            //
            string QStr = "SELECT RecID, SprID, NosID, Nos, Info, AccessU, AccessG, EditU FROM spr WHERE RecStatus=0 ORDER BY SprID, Nos";
            dsList = GFunc.GetData(QStr, "WebCNS", false);
            if (dsList.Tables.Count > 0)
            {
                if (dsList.Tables[0].Rows.Count > 0)
                {
                    thrNewRow = new TableHeaderRow();
                    thcNewCell = new TableHeaderCell();
                    thcNewCell.Text = "Labot";
                    thrNewRow.Cells.Add(thcNewCell);
                    foreach (DataColumn Col in dsList.Tables[0].Columns)
                    {
                        thcNewCell = new TableHeaderCell();
                        thcNewCell.Text = Col.ColumnName;
                        thrNewRow.Cells.Add(thcNewCell);
                    }
                    tSpr.Rows.AddAt(0, thrNewRow);

                    //CurrStruct = row["Strukturvieniba"].ToString();
                    //CurrNod = row["Nodala"].ToString();

                    foreach (DataRow row in dsList.Tables[0].Rows)
                    {
                        trNewRow = new TableRow();
                        tcNewCell = new TableCell();
                        if (Session["IsAdmin"].ToString() == "1")
                        {
                            cmdEdit = new Button();
                            cmdEdit.ID = "RecEdit" + row["RecID"].ToString();
                            cmdEdit.Text = "Labot";
                            cmdEdit.CssClass = "cmdEdit";
                            cmdEdit.CommandArgument = row["RecID"].ToString();
                            cmdEdit.PostBackUrl = "#pEdit";
                            cmdEdit.Click += new EventHandler(cmdEdit_Click);
                            tcNewCell.Controls.Add(cmdEdit);
                            Label BkmListG = new Label();
                            BkmListG.Text = "<a name=\"" + row["RecID"].ToString() + "\"></a>";
                            //BkmListG.Visible = false;
                            tcNewCell.Controls.Add(BkmListG);
                        }
                        trNewRow.Cells.Add(tcNewCell);
                        foreach (DataColumn Col in dsList.Tables[0].Columns)
                        {
                            tcNewCell = new TableCell();
                            tcNewCell.Text = row[Col].ToString() + "";
                            trNewRow.Cells.Add(tcNewCell);
                        }
                        tSpr.Rows.Add(trNewRow);

                    }
                }
            }
        }

        void cmdEdit_Click(object sender, EventArgs e)
        {
            string Rec2Edit;
            string QString;
            DataSet ds;
            gvAccessG.DataSource = GFunc.GetData("SELECT GroupName, Description FROM groups WHERE RecStatus=0 ORDER BY GroupName", "WebCNS", false);
            gvAccessG.DataBind();
            //gvAccessG.AutoGenerateSelectButton = true;
            gvAccessG.Visible = false;
            //dsUsers.ConnectionString = GFunc.GetConnectionString("WebCNS");
            gvAccessU.DataSource = GFunc.GetData("SELECT User, LongName, Groups FROM users WHERE RecStatus=0 ORDER BY User", "WebCNS", false);
            gvAccessU.DataBind();
            //gvAccessU.AutoGenerateSelectButton = true;
            gvAccessU.Visible = false;
            gvEditU.DataSource = GFunc.GetData("SELECT User, LongName, Groups FROM users WHERE RecStatus=0 ORDER BY User", "WebCNS", false);
            gvEditU.DataBind();
            //gvEditU.AutoGenerateSelectButton = true;
            gvEditU.Visible = false;
            //ddlSprID.Items.Clear();
            ddlSprID.ClearSelection();
            ddlSprID.DataSource=GFunc.GetData("SELECT SprID FROM spr WHERE RecStatus=0 GROUP BY SprID ORDER BY SprID","WebCNS",false);
            ddlSprID.DataValueField = "SprID";
            ddlSprID.DataTextField = "SprID";
            ddlSprID.DataBind();

            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            Rec2Edit = b.ID.Substring(7);
            QString = "SELECT * FROM spr WHERE RecStatus=0 AND RecID='" + Rec2Edit + "'";
            ds = GFunc.GetData(QString, "WebCNS", false);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    txtRecID.Text = row["RecID"].ToString();
                    ddlSprID.Text = row["SprID"].ToString();
                    txtNosID.Text = row["NosID"].ToString();
                    txtNos.Text = row["Nos"].ToString();
                    txtInfo.Text = row["Info"].ToString();
                    txtAccessU.Text = row["AccessU"].ToString();
                    txtAccessG.Text = row["AccessG"].ToString();
                    txtEditU.Text = row["EditU"].ToString();
                }
            }
            ddlSprID.Enabled = false;
            txtNosID.ReadOnly = true;
            cmdClose.PostBackUrl = "#" + b.CommandArgument;
            pEdit.Visible = true;

            //throw new NotImplementedException();
        }

        protected void cmdNew_Click(object sender, EventArgs e)
        {
            //dsGroups.ConnectionString = GFunc.GetConnectionString("WebCNS");
            gvAccessG.DataSource = GFunc.GetData("SELECT GroupName, Description FROM groups WHERE RecStatus=0 ORDER BY GroupName", "WebCNS", false);
            gvAccessG.DataBind();
            //gvAccessG.AutoGenerateSelectButton = true;
            gvAccessG.Visible = false;
            //dsUsers.ConnectionString = GFunc.GetConnectionString("WebCNS");
            gvAccessU.DataSource = GFunc.GetData("SELECT User, LongName, Groups FROM users WHERE RecStatus=0 ORDER BY User", "WebCNS", false);
            gvAccessU.DataBind();
            //gvAccessU.AutoGenerateSelectButton = true;
            gvAccessU.Visible = false;
            gvEditU.DataSource = GFunc.GetData("SELECT User, LongName, Groups FROM users WHERE RecStatus=0 ORDER BY User", "WebCNS", false);
            gvEditU.DataBind();
            //gvEditU.AutoGenerateSelectButton = true;
            gvEditU.Visible = false;
            //ddlSprID.Items.Clear();
            ddlSprID.ClearSelection();
            ddlSprID.DataSource=GFunc.GetData("SELECT SprID FROM spr WHERE RecStatus=0 GROUP BY SprID ORDER BY SprID","WebCNS",false);
            ddlSprID.DataValueField = "SprID";
            ddlSprID.DataTextField = "SprID";
            ddlSprID.DataBind();

            txtRecID.Text = "";
            txtNosID.Text = "";
            txtNos.Text = "";
            txtInfo.Text = "";
            txtAccessU.Text = "";
            txtAccessG.Text = "";
            txtEditU.Text = "";

            ddlSprID.Enabled = true;
            txtNosID.ReadOnly = true;
            pEdit.Visible = true;

        }

        protected void cmdClose_Click(object sender, EventArgs e)
        {
            lblErr.Text = "";
            pEdit.Visible = false;

        }

        protected void cmdDel_Click(object sender, EventArgs e)
        {
            string StrDel;

            lblErr.Text = "";
            StrDel = "UPDATE spr SET RecStatus=1 WHERE RecID='" + txtRecID.Text + "'";
            GFunc.GetData(StrDel, "WebCNS", false);
            lblErr.Text = "Saraksta dati ir izdzēsti.";
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string[] c = new string[11];
            string QString = "";
            string LastID = "";
            DataSet dsLastID;

            lblErr.Text = "";

            if (string.IsNullOrEmpty(txtNos.Text)==true)
            {
                lblErr.Text = "Nav norādīts ieraksta nosaukums!";
                return;
            }
            if (string.IsNullOrEmpty(ddlSprID.SelectedValue)==true)
            {
                lblErr.Text = "Nav norādīta saraksta grupa!";
                return;
            }
            try
            {
                if (string.IsNullOrEmpty(txtNosID.Text) == true)
                {
                    QString = "SELECT MAX(NosID)+1 as MaxID FROM spr WHERE RecStatus=0 AND SprID='" + ddlSprID.SelectedValue + "'";
                    dsLastID = GFunc.GetData(QString, "WebCNS", false);
                    txtNosID.Text = "0";
                    if (dsLastID.Tables.Count > 0)
                    {
                        if (dsLastID.Tables[0].Rows.Count > 0)
                        {
                            txtNosID.Text = dsLastID.Tables[0].Rows[0]["MaxID"].ToString();
                        }

                    }
                }
                c[0] = "RecID='" + GFunc.EscapeSQLCahrs(txtRecID.Text) + "'";
                c[1] = "SprID='" + GFunc.EscapeSQLCahrs(ddlSprID.SelectedValue) + "'";
                c[2] = "NosID='" + GFunc.EscapeSQLCahrs(txtNosID.Text) + "'";
                c[3] = "Nos='" + GFunc.EscapeSQLCahrs(txtNos.Text) + "'";
                c[4] = "Info='" + GFunc.EscapeSQLCahrs(txtInfo.Text) + "'";
                c[5] = "AccessU='" + GFunc.EscapeSQLCahrs(txtAccessU.Text) + "'";
                c[6] = "AccessG='" + GFunc.EscapeSQLCahrs(txtAccessG.Text) + "'";
                c[7] = "EditU='" + GFunc.EscapeSQLCahrs(txtEditU.Text) + "'";
                c[8] = "UserEdit='" + GFunc.EscapeSQLCahrs(Session["UserName"].ToString()) + "'";
                c[9] = "UserAdd='" + GFunc.EscapeSQLCahrs(Session["UserName"].ToString()) + "'";
                c[10] = "TimeAdd='" + GFunc.EscapeSQLCahrs(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) + "'";
                if (string.IsNullOrEmpty(txtRecID.Text) == false)
                {
                    QString = "UPDATE spr SET " + c[1] + "," + c[2] + "," + c[3] + "," + c[4] + "," + c[5] + "," + c[6] + "," + c[7] + "," + c[8] + " WHERE " + c[0];
                }
                else
                {
                    QString = "INSERT INTO spr SET " + c[1] + "," + c[2] + "," + c[3] + "," + c[4] + "," + c[5] + "," + c[6] + "," + c[7] + "," + c[8] + "," + c[9] + "," + c[10];
                }
                dsLastID = GFunc.GetData(QString, "WebCNS", true);
                if (txtRecID.Text.Length == 0)
                {

                    if (dsLastID.Tables.Count > 0)
                    {
                        if (dsLastID.Tables["tLID"].Rows.Count > 0)
                        {
                            txtRecID.Text = dsLastID.Tables["tLID"].Rows[0]["LID"].ToString();
                        }
                    }
                }
                lblErr.Text = "Saraksta dati ir saglabāti.";
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
        }

        protected void cmdShowU_Click(object sender, EventArgs e)
        {
            gvAccessU.Visible = !gvAccessU.Visible;
        }

        protected void cmdShowG_Click(object sender, EventArgs e)
        {
            gvAccessG.Visible = !gvAccessG.Visible;
        }

        protected void cmdShowEU_Click(object sender, EventArgs e)
        {
            gvEditU.Visible = !gvEditU.Visible;
        }

        protected void gvAccessU_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtAccessU.Text.Contains(";" + gvAccessU.SelectedRow.Cells[1].Text + ";"))
            {
                txtAccessU.Text = txtAccessU.Text.Replace(";" + gvAccessU.SelectedRow.Cells[1].Text + ";", ";");
            }
            else
            {
                txtAccessU.Text = txtAccessU.Text + ";" + gvAccessU.SelectedRow.Cells[1].Text + ";";
                txtAccessU.Text = txtAccessU.Text.Replace(";;", ";");
            }
        }

        protected void gvAccessG_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtAccessG.Text.Contains(";" + gvAccessG.SelectedRow.Cells[1].Text + ";"))
            {
                txtAccessG.Text = txtAccessG.Text.Replace(";" + gvAccessG.SelectedRow.Cells[1].Text + ";", ";");
            }
            else
            {
                txtAccessG.Text = txtAccessG.Text + ";" + gvAccessG.SelectedRow.Cells[1].Text + ";";
                txtAccessG.Text = txtAccessG.Text.Replace(";;", ";");
            }
        }

        protected void gvEditU_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtEditU.Text.Contains(";" + gvEditU.SelectedRow.Cells[1].Text + ";"))
            {
                txtEditU.Text = txtEditU.Text.Replace(";" + gvEditU.SelectedRow.Cells[1].Text + ";", ";");
            }
            else
            {
                txtEditU.Text = txtEditU.Text + ";" + gvEditU.SelectedRow.Cells[1].Text + ";";
                txtEditU.Text = txtEditU.Text.Replace(";;", ";");
            }
        }

        protected void ddlSprID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}
